package com.greate.springbootmysqlreadwrite.mysqlreadwrite.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Description 这里切换读/写模式
 * 原理是利用ThreadLocal保存当前线程是否处于读模式（通过开始READ_ONLY注解在开始操作前设置模式为读模式，
 * 操作结束后清除该数据，避免内存泄漏，同时也为了后续在该线程进行写操作时任然为读模式
 *
 * @author fxb
 * @date 2018-08-31
 */
public class DbContextHolder {

	private static Logger log = LoggerFactory.getLogger(DbContextHolder.class);
	public static final String WRITE = "write";
	public static final String READ = "read";

	private static ThreadLocal<String> contextHolder = new ThreadLocal<>();

	public static void setDbType(String dbType) {
		if (dbType == null) {
			log.error("dbType为空");
			throw new NullPointerException();
		}
		log.info("设置dbType为：{}", dbType);
		contextHolder.set(dbType);
	}

	public static String getDbType() {
		return contextHolder.get() == null ? WRITE : contextHolder.get();
	}

	public static void clearDbType() {
		contextHolder.remove();
	}
}